'use strict';

/**
 * author:Administrator
 * date:2015/12/1
 * desc: 模拟换脸
 */

var config = require('../../../config/index.js');
var Router = require('koa-router');
var appUtil = require('../../../common/app_utils.js');
var errCodes = require('../../../common/err_code.js');
var daoParts = require('../../../dao/face_part.js');
var daoUface = require('../../../dao/face_user.js');
var daoRendered = require('../../../dao/face_rendered.js');

//
var __pathPrefix = config.baseUrl + '/v1/face/render';
appUtil.appLog('router:' + __pathPrefix);
var router = new Router({prefix: __pathPrefix});
module.exports = router.routes();

/**
 * 进行换脸
 * post /api/v1/face/render
 {
   "uid": 123,
   "ufaceid": 1,
   "partids": "1,2,3,4"
 }
 * @return url 最终结果
 */
router.post('/', function *() {

  var uid = this.request.inParams.fields.uid;

  var ufaceid = this.request.inParams.fields.ufaceid;
  var partids = this.request.inParams.fields.partids;

  if (!ufaceid) {
    return this.body = appUtil.ferror(errCodes.E_ARGS_INVALID, 'ufaceid');
  }
  if (!partids) {
    return this.body = appUtil.ferror(errCodes.E_ARGS_INVALID, 'partids');
  }


  partids = partids.split(',');
  var parts = yield daoParts.getByPartids(partids);
  //appUtil.appLog(parts);

  var uface = yield daoUface.getOneById(ufaceid);
  //appUtil.appLog(uface);
  var saveRendered = {
    uid: uid,
    ufaceid: ufaceid,
    partids: partids
  };

  var renderRet = yield __doRender(parts, uface);
  appUtil.appLog(renderRet);
  if (renderRet.err) {
    yield daoRendered.save(saveRendered);
    return this.body = appUtil.ferror(errCodes.E_RENDER, renderRet.err);
  }

  var retBody = renderRet.body;
  if (typeof retBody === 'string')
    retBody = JSON.parse(retBody);
  //appUtil.appLog(retBody);
  var item = retBody.result.item[0];
  if (!item.filename) {
    yield daoRendered.save(saveRendered);
    return this.body = appUtil.ferror(errCodes.E_RENDER);
  }
  if (item.landmarks) {
    yield daoUface.updateFeatures(ufaceid, item.landmarks);
  }

  var url = config.renderResult + item.filename;
  // save to face_rendered
  saveRendered.url = url;
  saveRendered.rpath = item.filename;
  yield daoRendered.save(saveRendered);

  this.body = appUtil.fout({resultUrl: url});
});

/**
 * 这一部分内容与渲染的接口相关
 *
 * act, likeFace, userface, renderface
 * @param parts [{starid:1,otype:2},{...}]
 * @param uface userFace 模型对象
 * @private
 */
function * __doRender(parts, uface) {
  var likeFace = '';
  parts.forEach(item => {
    likeFace += item.starid + ',' + item.otype + ','
  });
  likeFace = likeFace.substr(0, likeFace.length - 1);

  var ufaceRpath = uface.rpath;
  if (config.env == 'development') {
    ufaceRpath = '2015-12-01/123_18-42-00_uuid.jpg'; //debug
  }
  var outpath = appUtil.getPath_rendered(ufaceRpath);
  var form = {
    act: 1,
    likeFace: likeFace, // 1,2, 2,3 => (1,2) 和 (2,3) 代表1号明星的2部位 以及 2号明星的3部位
    userface: ufaceRpath, // 用户图片位置
    renderface: outpath // 渲染后的图片位置
  };
  //appUtil.appLog(form);

  var result = yield appUtil.makeRequest_postForm(config.renderUrl, form);
  //appUtil.appLog(result);
  if (typeof result === 'string')
    result = JSON.parse(result);
  // result.resultUrl = config.renderResult + outpath;
  return result;
}